<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="报警类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="policeType">
<!--              <j-dict-select-tag type="list" v-model="model.policeType" dictCode="rule_status" placeholder="请选择状态 0生效 1失效" />-->
                  <a-select v-model="model.policeType" placeholder="请选择报警类型">
                    <a-select-option :value="1">液位报警</a-select-option>
                    <a-select-option :value="2">水质报警</a-select-option>
                  </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规则名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleName">
              <a-input v-model="model.ruleName" placeholder="请输入规则名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规则描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleDesc">
              <a-input v-model="model.ruleDesc" placeholder="请输入规则描述"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="生效区域" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaId">
              <j-select-depart v-model="model.areaId" :multi="true"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="equType">
              <j-dict-select-tag type="list" v-model="model.equType" dictCode="sb_type" placeholder="请选择设备类型" @change="gatData" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="报警级别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="level">
              <j-dict-select-tag ct-tag type="list" v-model="model.level" dictCode="ps_police_lev" placeholder="请选择报警级别" />
            </a-form-model-item>
          </a-col>


          <div v-if="ruleShow">
            <a-col :span="24">
              <a-form-model-item label="规则内容" :colon="false" :labelCol="labelCol" :wrapperCol="wrapperCol"></a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item :label="title" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contentField">
                <j-dict-select-tag type="list" v-model="model.contentField" dictCode="ps_rule_type" placeholder="请输入规则内容字段"></j-dict-select-tag>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label=" " :colon="false" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contentOperator">
                <j-dict-select-tag type="list" v-model="model.contentOperator" dictCode="ps_operator" placeholder="请选择规则内容比较符" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label=" " :colon="false" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contentLimit">
                <a-input-number v-model="model.contentLimit" placeholder="请输入规则内容阈值" style="width: 100%" />
              </a-form-model-item>
            </a-col>
<!--            <a-col :span="24">-->
<!--              <a-form-model-item label="报警字段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">-->
<!--                <a-input v-model="model.content" placeholder="请输入报警字段"  ></a-input>-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
<!--            <a-col :span="24">-->
<!--              <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">-->
<!--                <j-dict-select-tag type="list" v-model="model.status" dictCode="ps_rule_status" placeholder="请选择状态" />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
          </div>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'PaishuiRuleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/paishui/paishuiRule/add",
          edit: "/paishui/paishuiRule/edit",
          queryById: "/paishui/paishuiRule/queryById"
        },
        ruleShow:false,
        typeNum:'',
        title:'流量计'
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      gatData(value){
        console.log('getData',value)
        // this.typeNum = value
        switch (value) {
          case '1' :
            this.title = '液位计'
            this.ruleShow = true
            break
          case '2' :
            this.title = '水质监测'
            this.ruleShow = true
            break
          case '3' :
            this.title = '流量计'
            this.ruleShow = true
            break
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        console.log('record',record)
        this.model = Object.assign({}, record);
        this.visible = true;
        switch (record.equType) {
          case 1 :
            this.title = '液位计'
            this.ruleShow = true
            break
          case 2 :
            this.title = '水质监测'
            this.ruleShow = true
            break
          case 3 :
            this.title = '流量计'
            this.ruleShow = true
            break
        }
        // this.ruleShow = true
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model.status = 0
            console.log('this.model',this.model)
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>